﻿using AES小工具.LinuxLogToolPackage.sysstat;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace AES小工具
{
    public partial class FrmLinuxToolLog : Form
    {
        public FrmLinuxToolLog()
        {
            InitializeComponent();
        }

        private string[] readFileDatas() {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择文件";
            fileDialog.Filter = "所有文件(*log*)|*.log*"; //设置要选择的文件的类型
            if (fileDialog.ShowDialog() != DialogResult.OK)
            {
                //MessageBox.Show("没有选中文件");
                return null;
            }

            string filePath = fileDialog.FileName;//返回文件的完整路径
            DateTime startDt = DateTime.Now;
            //读取文件
            string[] logs = System.IO.File.ReadAllLines(filePath);
            return logs;
        }

        private void getHeadInfo(string headInfo)
        {
            string[] headArr = headInfo.Split(new char[] { '\t' }); //Replace('\t', ',');
            laKernel.Text = "内核:" + headArr[0];
            laRecordTime.Text = "时间:" + headArr[1];
            laCpus.Text = "CPU数:" + headArr[3];
        }

        private void btnAnalysisSarU_Click(object sender, EventArgs e)
        {
            string[] logs = readFileDatas();
            if (logs == null) {
                MessageBox.Show("没有选中文件，或文件内容为空!");
                return;
            }
            cleanLogs();
            getHeadInfo(logs[1]);
            string title = logs[3];
            List<SarU> sarUs = new List<SarU>();
            /*double IdAvg15Min = 0;
            double IdAvg15Max = 0;
            double IdAvg15 = 0;
            double IdAvgTotal = 0;*/

            double userMin = 0;
            double userMax = 0;
            double userAvg = 0;
            double userTotal = 0;
            for (int i = 4; i < logs.Length; i++)
            {
                string log = logs[i];
                //char[] chars = logs[i].ToCharArray();
                log = log.Replace("        ", ",");
                log = log.Replace("       ", ",");
                log = log.Replace("      ", ",");
                log = log.Replace("     ", ",");
                string[] logArr = log.Split(new char[] { ','});
                SarU sarU = new SarU(double.Parse(logArr[2]), double.Parse(logArr[3]), double.Parse(logArr[4]),
                    double.Parse(logArr[5]), double.Parse(logArr[6]), double.Parse(logArr[7]), 
                    DateTime.Parse(logArr[0]));
                if (userMin == 0 || userMin > sarU.User) { 
                    userMin = sarU.User;
                }
                if (userMax < sarU.User) { 
                    userMax = sarU.User;
                }
                userTotal += sarU.User;
                sarUs.Add(sarU);
            }
            userAvg = userTotal / sarUs.Count;

            laMinVal.Text = "最小值:" + userMin;
            laMaxVal.Text = "最大值:" + userMax;
            laAvgVal.Text = "平均值:" + userAvg.ToString("0.000");
            //升序
            //sarUs = sarUs.OrderBy(x=>x.User).ToList();
            sarUs.Sort((s, x) => s.User.CompareTo(x.User));
            //从小到大取200个值
            showLogs("=========================  从小到大取200行显示  =========================");
            showSarULogs(sarUs,title);
            //降序
            sarUs.Sort((s,x) => x.User.CompareTo(s.User));
            showLogs("\r\n=========================  从大到小取200行显示  =========================\r\n");
            showSarULogs(sarUs, title);
        }

        private void showSarULogs(List<SarU> sarUs,string title) {

            if (sarUs.IsNullOrEmpty() || sarUs.Count == 0)
                return;

            string log = string.Empty;
            for (int i = 0; i < sarUs.Count && i < 500; i++)
            {
                if (i > 0 && i % 10 == 0)
                {
                    showLogs(title);
                    showLogs(log);
                    log = string.Empty;
                    
                }

                if (!log.IsNullOrEmpty())
                {
                    log += "\r\n";
                }
                log += sarUs[i].CreateTime.ToString("HH:mm:ss") + "\t" + "all" + "\t" + sarUs[i].User + "\t"
                    + sarUs[i].Nice + "\t" + sarUs[i].System + "\t" + sarUs[i].IoWait + "\t" + sarUs[i].steal + "\t"
                    + sarUs[i].Idle;
            }
            if (!log.IsNullOrEmpty())
            {
                showLogs(title);
                showLogs(log);
            }
        }

        private void btnAnalysisSarQ_Click(object sender, EventArgs e)
        {
            string[] logs = readFileDatas();
            if (logs == null)
            {
                MessageBox.Show("没有选中文件，或文件内容为空!");
                return;
            }
            cleanLogs();
            getHeadInfo(logs[1]);
            string title = logs[3];
            double IdAvg15Min = 0;
            double IdAvg15Max = 0;
            double IdAvg15 = 0;
            double IdAvgTotal15 = 0;

            double IdAvg5Min = 0;
            double IdAvg5Max = 0;
            double IdAvg5 = 0;
            double IdAvgTotal5 = 0;

            double IdAvg1Min = 0;
            double IdAvg1Max = 0;
            double IdAvg1 = 0;
            double IdAvgTotal1 = 0;

            List<SarQ> sarQs = new List<SarQ>();

            for (int i = 4; i < logs.Length; i++) {
                string log = logs[i];
                //char[] chars = logs[i].ToCharArray();
                log = log.Replace("        ", ",");
                log = log.Replace("       ", ",");
                log = log.Replace("      ", ",");
                log = log.Replace("     ", ",");
                string[] logArr = log.Split(new char[] { ',' });
                SarQ sarQ = new SarQ(int.Parse(logArr[1]), int.Parse(logArr[2]), double.Parse(logArr[3]),
                    double.Parse(logArr[4]),double.Parse(logArr[5]),double.Parse(logArr[6]),
                    DateTime.Parse(logArr[0]));
                if (IdAvg15Min == 0 || IdAvg15Min > sarQ.IdAvg15) { 
                    IdAvg15Min = sarQ.IdAvg15;
                }
                if(IdAvg15Max < sarQ.IdAvg15)
                {
                    IdAvg15Max = sarQ.IdAvg15;
                }
                IdAvgTotal15 += sarQ.IdAvg15;

                if (IdAvg5Min == 0 || IdAvg5Min > sarQ.IdAvg5)
                {
                    IdAvg5Min = sarQ.IdAvg5;
                }
                if (IdAvg5Max < sarQ.IdAvg5)
                {
                    IdAvg5Max = sarQ.IdAvg5;
                }
                IdAvgTotal5 += sarQ.IdAvg5;

                if (IdAvg1Min == 0 || IdAvg1Min > sarQ.IdAvg1)
                {
                    IdAvg1Min = sarQ.IdAvg1;
                }
                if (IdAvg1Max < sarQ.IdAvg1)
                {
                    IdAvg1Max = sarQ.IdAvg1;
                }
                IdAvgTotal1 += sarQ.IdAvg1;

                sarQs.Add(sarQ);
            }
            IdAvg15 = IdAvgTotal15 / sarQs.Count;
            IdAvg5 = IdAvgTotal5 / sarQs.Count;
            IdAvg1 = IdAvgTotal1 / sarQs.Count;

            laMinVal.Text = "最小值:" + IdAvg1Min + "  "+ IdAvg5Min + "  " +  IdAvg15Min;
            laMaxVal.Text = "最大值:" + IdAvg1Max + "  " + IdAvg5Max + "  " + IdAvg15Max;
            laAvgVal.Text = "平均值:" + IdAvg1.ToString("0.000") + "  " + IdAvg5.ToString("0.000") + "  " + IdAvg15.ToString("0.000");
            sarQs = sarQs.OrderBy(x => x.IdAvg5).ToList();
            showLogs("=========================  从小到大取200行显示  =========================");
            showSarQLogs(sarQs, title);
            sarQs.Sort((s, x) => x.IdAvg5.CompareTo(s.IdAvg5));
            showLogs("\r\n=========================  从大到小取200行显示  =========================\r\n");
            showSarQLogs(sarQs, title);
        }

        private void showSarQLogs(List<SarQ> sarQs,string title)
        {
            string log = string.Empty;
            for (int i = 0; i < sarQs.Count && i < 500; i++)
            {
                if (i > 0 && i % 10 == 0)
                {
                    showLogs(title);
                    showLogs(log);
                    log = string.Empty;
                }
                if (!log.IsNullOrEmpty())
                {
                    log += "\r\n";
                }

                log += sarQs[i].CreateTime.ToString("HH:mm:ss") + "\t" + sarQs[i].RunQSz + "\t"
                    + sarQs[i].PlistSz + "\t" + sarQs[i].IdAvg1 + "\t" + sarQs[i].IdAvg5 + "\t" + sarQs[i].IdAvg15 + "\t"
                    + sarQs[i].Blocked;
            }
            if (!log.IsNullOrEmpty())
            {
                showLogs(title);
                showLogs(log);
            }
        }

        private void showLogs(string log) { 
            tbLogs.Text += log + "\r\n";
        }

        private void cleanLogs()
        {
            tbLogs.Text = "";
        }


    }
}
